<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改商品')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-product-edit" th:object="${aProduct}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">商品名称：</label>
                    <div class="col-sm-8">
                        <input name="productName" th:field="*{productName}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">商品分类：</label>
                    <div class="col-sm-8">
                        <select name="categoryId" id="productCategory" class="form-control" required>
                            <!-- 分类选项将通过 JavaScript 动态填充 -->
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">图片：</label>
                    <!--                 onclick="$.triggerFileInput()"-->
                    <div class="UploadImg">  <!-- 为整个区域添加点击事件 -->
                        <div class="imgList" id="imgList">
                            <!-- 这里用于显示已上传的图片或加载状态 -->
                            <img th:if="${aProduct.imgUrl}" th:src="${aProduct.imgUrl}" width="100" height="100" border-radius="10" border="1px solid #d9d9d9" />
                        </div>
                        <!--                    <div class="addBtn" id="addBtn" >-->
                        <!--                        <i class="fa fa-plus"></i>-->
                        <!--                    </div>-->
                    </div>
                    <!--                 onchange="$.uploadImage()"-->
                    <input type="file" id="fileInput" style="display: none;"/>
                    <!-- 隐藏的文件输入元素，并绑定 onchange 事件 -->
                    <input name="imgUrl" id="imgUrl" th:field="*{imgUrl}" class="form-control" type="hidden">
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">售价：</label>
                    <div class="col-sm-8">
                        <input name="productPrice" th:field="*{productPrice}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">商品积分：</label>
                    <div class="col-sm-8">
                        <input name="productScore" th:field="*{productScore}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">库存：</label>
                    <div class="col-sm-8">
                        <input name="productStock" th:field="*{productStock}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">


        var prefix = ctx + "system/product";
        var uploadUrl = ctx + "common/upload";
        var categoryUrl = ctx + "system/category/list";

        $(document).ready(function() {
            loadCategories();
            $("#form-product-edit").validate({
                focusCleanup: true
            });

            $.fileUpload.init();
        });

        function loadCategories() {
            $.ajax({
                url: categoryUrl,
                type: 'POST',
                dataType: 'json',
                success: function(data) {
                    console.log("data:", data.rows);
                    var $categorySelect = $('#productCategory');
                    console.log($categorySelect);
                    $categorySelect.empty(); // 清空现有选项
                    if (data && data.rows && Array.isArray(data.rows)) {
                        data.rows.forEach(function(category) {
                            console.log(category);
                            var option = $('<option></option>')
                                .attr('value', category.id)
                                .text(category.name);
                            $categorySelect.append(option);
                            console.log($categorySelect);
                        });
                    }
                },
                error: function(xhr, status, error) {
                    console.error('加载分类数据失败:', error);
                }
            });
        }

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-product-edit').serialize());
            }
        }
    </script>
</body>
</html>